/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.axiom.persistence;

import com.axiom.utility.IdGenerator;
import com.axiom.vo.instructor_detail;
import com.axiom.vo.instuctor_course;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Shuhail
 */
public class instructor_detail_persist {
    
    Session sess = null;
    Transaction tx = null;
    
    public static instructor_detail_persist getInstance() {
        
        return new instructor_detail_persist();
    }
    
    public instructor_detail_persist() {
        
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        sess = sf.openSession();
        tx = sess.beginTransaction();
    }
    
    public boolean insertFresh(com.axiom.vo.instructor_detail detail) {
        
        try {
            
            detail.setInstructor_id(IdGenerator.getInstance().getValue(this.getTheLastPK()));
            sess.save(detail);
            tx.commit();
            return true;
        }
        catch(Exception e) {
            
            return false;
        }
        
        
    }
    
    
    public List getAllInstructor() {
        
        Query query = sess.getNamedQuery("getAllInstructorDetails");
        return query.list();
    }
    
    
    public String getTheLastPK() {
        
        List list = this.getAllInstructor();
        Iterator it = list.iterator();
        List<String> pkList = new ArrayList<String>();
        while(it.hasNext())  {
            
            instructor_detail detail = (instructor_detail)it.next();
            pkList.add(detail.getInstructor_id());
        }
        
        String lastPK=IdGenerator.getInstance().getTheLastPK(pkList);
        return lastPK;
    }
    
    
    public ScrollableResults getIntructorDetailAndCourse() {
        
        Query query = sess.getNamedQuery("getInstructorAndCourseDetails");
        ScrollableResults sr = query.scroll();
        if(!sr.next()) {
            return null;
        }
        return sr;
    }
    
    
    public int deleteInstructorBasedOnId(String inst_id) {
        
        Query query = sess.getNamedQuery("deleteInstructorBasedOnId");
        query.setParameter("inst_id", inst_id);
        try {
            
            int delRows = query.executeUpdate();
            return delRows;
            
        }
        
        catch(Exception e) {
            return 0;
        }
    }
    public static void main(String[] args) {
        
        com.axiom.vo.instructor_detail detail = new com.axiom.vo.instructor_detail();
        //detail.setInstructor_id("I1");
      //  detail.setName("SHu 1");
       // detail.setPhone("12312");
        //detail.setDate(new Date());
      //ScrollableResults sr =  new instructor_detail_persist().getIntructorDetailAndCourse();
      
        System.out.println(new instructor_detail_persist().deleteInstructorBasedOnId("I11"));
    }
}
